package vn.cent.games.findnumber.beans;

import vn.cent.games.findnumber.utils.Constants;

/**
 * 
 * @author TV.Vinh
 * @version 1.0
 *
 */
public class GameMapBean {
	
	/** 2-dimension enum array save cells state corresponding to game map(client-side) */
	PositionState[] map_state;
	
	/** number of turns that both player made
	 * when it reached to 99, the game should be stopped */
	private int turns = 0;
	
	public GameMapBean() {
		InitMapState();
	}
	
	/** set all cells in map to non-occupied */
	public void InitMapState(){
		map_state = new PositionState[Constants.MAX_TURNS];
		for (int i = 0; i < Constants.MAX_TURNS; i++) {
			map_state[i] = PositionState.NON_OCCUPIED;
		}
	}
	
	/**
	 * update cell in map
	 * 
	 * @param state: no-one, occupied by p1 or p2
	 * @param x: x position
	 * @param y: y position
	 */
	public boolean setState(PositionState state, int x){
		synchronized (map_state) {
			if (map_state[x] == PositionState.NON_OCCUPIED) {
				map_state[x] = state;
				turns++;
				return true;
			} else {
				return false;
			}
		}
		
	}
	
//	/**
//	 * 
//	 * 
//	 * @return
//	 */
//	public int getWinner(){
//		int p1 = 0, p2 = 0;
//		
//		for (int i = 0; i < Constants.MAX_TURNS; i++) {
//			if (map_state[i] == PositionState.P1_OCCUPIED) {
//				p1++;
//			}
//		}
//		p2 = Constants.MAX_TURNS - p1;
//		
//		if(p1 > p2){
//			return 1;
//		} else if(p1 < p2){
//			return 2;
//		} else {
//			return 0;
//		}
//		
//	}
	
	// GETTER & SETTER
	public int getTurns() {
		return turns;
	}

}
